var WarehouseInfoRecord = function () {
  var template = /* html */ `
    <div class="warehouse-info-record" v-loading.fullscreen.lock="loading">
      <div style="width:100%;">
        <el-table show-summary fit border class="table-in-summary" :data="records" :summary-method="summaryMethod">
          <el-table-column :resizable="false" prop="materialName" label="物料名称" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="model" label="规格" :auto-width="true" min-width="60px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="color" label="编号" :auto-width="true" min-width="60px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="providerName" label="供应商" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="inoutTypeText" label="出入库类型" width="80px" align="center"></el-table-column>
          <el-table-column :resizable="false" prop="inCount" label="入库数" :auto-width="true" min-width="60px" align="center"></el-table-column>
          <el-table-column :resizable="false" prop="outCount" label="出库数" :auto-width="true" min-width="60px" align="center"></el-table-column>
          <el-table-column :resizable="false" prop="orderId" label="系统订单编号" :auto-width="true" min-width="100px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="companyOrderId" label="公司订单编号" :auto-width="true" min-width="100px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="factoryNo" label="工厂型号" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="customerNo" label="客户型号" :auto-width="true" min-width="70px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="position" label="仓位" :auto-width="true" min-width="60px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" column-key="createTime" label="制单时间" min-width="85px" align="center">
            <template slot-scope="scope">
              <span>{{moment(scope.row.createTime).format('YYYY-MM-DD')}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" prop="createUser" label="制单人" :auto-width="true" min-width="60px" header-align="center"></el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      warehouse: {
        type: Object,
        required: true
      }
    },

    data: function () {
      return {
        loading: false,
        records: [],
        moment: moment
      };
    },

    created: function () {
      this.setRecords();
    },

    methods: {
      setRecords: function () {
        var warehouse = this.warehouse;
        var warehouseType = warehouse.warehouseType;
        var postData = {
          status: 1,
          warehouseType: warehouseType,
          materialCode: warehouse.materialCode
        };

        this.loading = true;
        var self = this;
        ajaxRequest("warehouse/getWarehouseInOutDetaliListByMaterial", postData).then(function (records) {
          for (var i = 0; i < records.length; i++) {
            var record = records[i];
            var inoutTypeText;
            if ([1, 3].indexOf(record.source) !== -1 || [1, 3, 4, 7, 8, 11].indexOf(record.type) !== -1) {
              inoutTypeText = '入库';
              record.inCount = record.count;
              record.outCount = '';
            } else {
              inoutTypeText = '出库';
              record.inCount = '';
              record.outCount = record.count;
            }
            record.inoutTypeText = inoutTypeText;
            record.createTime = moment(record.createTime).valueOf();
          }

          self.records = records;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      summaryMethod: function (param) {
        var columns = param.columns;
        var data = param.data;
        var sums = ['合计'];
        var sumKeys = ['inCount', 'outCount'];
        var sumObj = {};
        for (var i = 1; i < columns.length - 7; i++) {
          var column = columns[i];
          var columnKey = column.columnKey;
          var sum = null;
          if (sumKeys.indexOf(columnKey) !== -1) {
            var property = column.property;
            var sum = _.sumBy(data, function (obj) {
              var value = obj[property];
              if (value === '') {
                return 0;
              }
              return value;
            });
            sumObj[columnKey] = sum;
          }
          sums.push(_.round(sum, 4));
        }
        var stockSum = sumObj.inCount - sumObj.outCount;
        if (stockSum < 0) {
          stockSum = 0;
        }
        stockSum = _.round(stockSum, 4);
        sums.push({
          span: 7,
          value: `库存数:${stockSum}`,
          align: 'left'
        });
        return sums;
      }
    },

    template: template
  };
}();